package com.jumi.microservice.service.logistics.express;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jumi.microservice.dto.logistics.LogisticsSearchRequest;
import com.jumi.microservice.dto.logistics.LogisticsSearchResponse;
import com.sf.csim.express.service.CallExpressServiceTools;
import com.sf.csim.express.service.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @author by mazhao
 * 百世快递
 * @Classname BestExpress
 * @Description TODO
 * @Date 2020/8/21 14:30
 */
@Service
public class SfExpress extends ResponseController implements Express {

    private static final Logger log = LoggerFactory.getLogger(SfExpress.class);

    /**
     * 顾客编码
     */
    private static final String CLIENT_CODE = "JMDZS9";
    /**
     * 此处替换为您在丰桥平台获取的校验码
     */
    private static final String CHECK_WORD = "siuK8I3ifNjjbA7o8xXuLtgr6iEZA0mw";

    /**
     * 接口服务代码
     */
    private static final String serviceCode = "EXP_RECE_SEARCH_ROUTES";

    /**
     * 沙箱环境的地址
     */
//    private static final String CALL_URL_BOX = "https://sfapi-sbox.sf-express.com/std/service";

    /**
     * 生产环境的地址
     */
    private static final String CALL_URL_PROD = "https://sfapi.sf-express.com/std/service";

    @Override
    public String expressCode() {
        return "SF";
    }

    @Override
    public LogisticsSearchResponse logisticsSearch(LogisticsSearchRequest request) {
        log.info("顺丰查询参数[{}]", JSON.toJSONString(request));
        JSONArray jsonArray = new JSONArray();
        try {
            Map<String, String> params = new HashMap<>();
            String msgData = "{" +
                    "    \"language\": 0," +
                    "    \"trackingType\": 1," +
                    "    \"trackingNumber\": [\""+request.getLogisticsNo()+"\"]," +
                    "    \"methodType\": 1" +
                    "}";
            String timeStamp = String.valueOf(System.currentTimeMillis());
            params.put("partnerID", CLIENT_CODE);
            params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
            params.put("serviceCode",serviceCode);
            params.put("timestamp", timeStamp);
            params.put("msgData", msgData);
            params.put("msgDigest", CallExpressServiceTools.getMsgDigest(msgData,timeStamp,CHECK_WORD));
            String res = HttpClientUtil.post(CALL_URL_PROD, params);
            log.info("请求返回结果[{}]",res);
            JSONObject jsonObject = JSONObject.parseObject(res.replace("\"{","{")
                    .replace("}\"","}").replace("\\",""));
            JSONObject  json  = jsonObject.getJSONObject("apiResultData");
            json = json.getJSONObject("msgData").getJSONArray("routeResps").getJSONObject(0);
            jsonArray = json.getJSONArray("routes");
        } catch (Exception e) {
            log.error("顺丰查询出错[{}]",e.getMessage());
        }
        LogisticsSearchResponse logisticsSearchResponse = checkResult(jsonArray,request);
        return controller(logisticsSearchResponse,jsonArray,"remark","acceptTime");
    }

}
